package cn.edu.sdjzu.xg23.kcsj01_backend.controller;

import cn.edu.sdjzu.xg23.kcsj01_backend.entity.ApprovalState;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.Conference;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.Employee;
import cn.edu.sdjzu.xg23.kcsj01_backend.entity.ParticipatingConference;
import cn.edu.sdjzu.xg23.kcsj01_backend.service.impl.ParticipatingConferenceServiceImpl;
import cn.edu.sdjzu.xg23.kcsj01_backend.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/participationConference")
public class ParticipationConferenceController {

    @Autowired
    private ParticipatingConferenceServiceImpl participatingConferenceServiceImpl;

    @PreAuthorize("hasAuthority('participatingConference:add')")
    @PostMapping
    public Result addParticipatingConference(@RequestBody ParticipatingConference participatingConference) {
        boolean isSuccess = participatingConferenceServiceImpl.addParticipatingConference(participatingConference);
        if (isSuccess) {
            return Result.success("保存成功");
        }
        return Result.error("006", "保存失败");
    }

    @PreAuthorize("hasAuthority('participatingConference:update')")
    @PutMapping
    public Result updateParticipatingConference(@RequestBody ParticipatingConference participatingConference) {
        boolean isSuccess = participatingConferenceServiceImpl.updateParticipatingConference(participatingConference);
        if (isSuccess) {
            return Result.success("更新成功");
        }
        return Result.error("008", "更新失败");
    }

    @PreAuthorize("hasAuthority('participatingConference:delete')")
    @DeleteMapping("/{participationConferenceId}")
    public Result deleteParticipatingConference(@PathVariable Long participationConferenceId) {
        boolean isSuccess = participatingConferenceServiceImpl.deleteParticipatingConference(participationConferenceId);
        if (isSuccess) {
            return Result.success("删除成功");
        }
        return Result.error("007", "删除失败");
    }

    @PreAuthorize("hasAuthority('participatingConference:search:all')or hasAuthority('participatingConference:search:my')")
    @GetMapping
    public Result selectParticipatingConferencesByCondition(
            @RequestParam(defaultValue = "1") Integer pageNum,  // 改为Integer包装类型
            @RequestParam(defaultValue = "10") Integer pageSize, // 改为Integer包装类型
            @ModelAttribute ParticipatingConference participatingConference,
            @RequestParam(required = false) String conferenceName,
            @RequestParam(required = false) String applicantName,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date reportTime,
            @RequestParam(required = false) Long approvalState
    ) {

        PageInfo<ParticipatingConference> pageInfo =
                participatingConferenceServiceImpl.selectParticipatingConferencesByCondition(pageNum, pageSize, participatingConference);
        return Result.success(pageInfo);
    }

    @PreAuthorize("hasAuthority('participatingConference:approve')")
    @PutMapping("/approve/{id}")
    public Result approveParticipatingConference(@PathVariable Long id, @RequestParam Long approvalCode) {
        boolean isSuccess = participatingConferenceServiceImpl.approvalParticipatingConference(id, approvalCode);
        if (isSuccess) {
            return Result.success("审批状态更新成功");
        }
        return Result.error("009", "审批状态更新失败");
    }
}